package com.example.administrator.szb.fragments.fragment_forTab;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.bumptech.glide.Glide;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.AskMannagerActivity;
import com.example.administrator.szb.activity.GZXYActivity;
import com.example.administrator.szb.activity.GuanZhuActivity;
import com.example.administrator.szb.activity.MessageActivity;
import com.example.administrator.szb.activity.MyInfoActivity;
import com.example.administrator.szb.activity.MySuPeiActivity;
import com.example.administrator.szb.activity.MyXM2Activity;
import com.example.administrator.szb.activity.SZActivity;
import com.example.administrator.szb.activity.ShouCangTmp;
import com.example.administrator.szb.activity.base.BasePresenter;
import com.example.administrator.szb.activity.luyan.MyLuYanActivity;
import com.example.administrator.szb.bean.UserInfo2;
import com.example.administrator.szb.common.SetStatusBar;
import com.example.administrator.szb.fragments.base.MVPBaseFragment;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.HttpUtils;
import com.example.administrator.szb.http.OnResultListener;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.DialogUtil;
import com.example.administrator.szb.util.SPUtils;

import java.util.HashMap;
import java.util.Map;


/**
 * 普通用户
 * Created by Administrator on 2017/9/5.
 */

public class FragmentMy2 extends MVPBaseFragment implements View.OnClickListener {

    private ImageView head_img;
    private TextView nick_name;
    private TextView qian_name;
    private TextView tv_status;
    private android.support.v7.widget.CardView cardView;
    private LinearLayout my_ask_ll;
    private TextView my_ask_text;
    private LinearLayout my_answer_ll;
    private TextView my_answer_text;
    private LinearLayout my_guanzhu_ll;
    private TextView my_guanzhu_text;
    private LinearLayout guanzhu_my_ll;
    private TextView guanzhu_my_text;
    private LinearLayout my_project;
    private LinearLayout my_collection,go_info_ll;
    private LinearLayout my_message;
    private LinearLayout about_us,supei_ll;
    private LinearLayout setting,luyan;
    private SwipeRefreshLayout swiperefreshlayout;
    UserInfo2 userInfo2;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_mine2, null);
    }

    @Override
    protected void initView(View view) {
        SetStatusBar.setBar(view,getContext());
        supei_ll = view.findViewById(R.id.supei_ll);
        supei_ll.setOnClickListener(this);
        luyan = view.findViewById(R.id.luyan);
        go_info_ll = view.findViewById(R.id.go_info_ll);
        swiperefreshlayout = view.findViewById(R.id.swiperefreshlayout);
        head_img = (ImageView) view.findViewById(R.id.head_img);
        nick_name = (TextView) view.findViewById(R.id.nick_name);
        qian_name = (TextView) view.findViewById(R.id.qian_name);
        tv_status = (TextView) view.findViewById(R.id.tv_status);
        cardView = (android.support.v7.widget.CardView) view.findViewById(R.id.cardView);
        my_ask_ll = (LinearLayout) view.findViewById(R.id.my_ask_ll);
        my_ask_text = (TextView) view.findViewById(R.id.my_ask_text);
        my_answer_ll = (LinearLayout) view.findViewById(R.id.my_answer_ll);
        my_answer_text = (TextView) view.findViewById(R.id.my_answer_text);
        my_guanzhu_ll = (LinearLayout) view.findViewById(R.id.my_guanzhu_ll);
        my_guanzhu_text = (TextView) view.findViewById(R.id.my_guanzhu_text);
        guanzhu_my_ll = (LinearLayout) view.findViewById(R.id.guanzhu_my_ll);
        guanzhu_my_text = (TextView) view.findViewById(R.id.guanzhu_my_text);
        my_project = (LinearLayout) view.findViewById(R.id.my_project);
        my_collection = (LinearLayout) view.findViewById(R.id.my_collection);
        my_message = (LinearLayout) view.findViewById(R.id.my_message);
        about_us = (LinearLayout) view.findViewById(R.id.about_us);
        setting = (LinearLayout) view.findViewById(R.id.setting);

    }

    @Override
    protected void initEvent() {
        swiperefreshlayout.setRefreshing(true);
        swiperefreshlayout.setProgressBackgroundColorSchemeResource(android.R.color.white);
        // 设置下拉进度的主题颜色
        swiperefreshlayout.setColorSchemeResources(R.color.colorAccent, R.color.colorPrimary, R.color.colorPrimaryDark);
// 下拉时触发SwipeRefreshLayout的下拉动画，动画完毕之后就会回调这个方法
        swiperefreshlayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                requestUserInfo();

            }
        });
        tv_status.setOnClickListener(this);
        setting.setOnClickListener(this);
        my_ask_ll.setOnClickListener(this);
        my_answer_ll.setOnClickListener(this);
        my_guanzhu_ll.setOnClickListener(this);
        guanzhu_my_ll.setOnClickListener(this);
        my_project.setOnClickListener(this);
        my_collection.setOnClickListener(this);
        about_us.setOnClickListener(this);
        my_message.setOnClickListener(this);
        go_info_ll.setOnClickListener(this);

        luyan.setOnClickListener(this);
    }


    @Override
    protected void initData() {
        if (SPUtils.getUser()!=null){
            userInfo2 = SPUtils.getUser();
            initUser();
        }
        //requestUserInfo();
    }
    private void initUser(){
        Glide.with(context).load(userInfo2.getHeadimg()).error(R.mipmap.default_header_s).into(head_img);
        nick_name.setText(userInfo2.getNick_name());
        if (userInfo2.getSketch()==null){
            userInfo2.setSketch("尚未设置个性签名");
        }
        qian_name.setText((String)userInfo2.getSketch());
        String str = "";
        switch (userInfo2.getStatus()){
            case 1:
                str="未认证";
                break;
            case 2:
                str="认证审核中";
                break;
            case 3:
                str="审核通过";
                break;
            case 4:
                str="未通过";
                break;
            case 5:
                str="禁用";
                break;
            default:
                str="尚未认证";
                break;
        }
        tv_status.setText(str);
        my_ask_text.setText(""+userInfo2.getAskRight());
        my_answer_text.setText(""+userInfo2.getAskLeft());
        my_guanzhu_text.setText(""+userInfo2.getAttenRight());
        guanzhu_my_text.setText(""+userInfo2.getAttenLeft());

    }


    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()){
            case R.id.supei_ll:

                intent = new Intent(context, MySuPeiActivity.class);
                startActivity(intent);
                break;
            case R.id.luyan:
                intent = new Intent(getActivity(), MyLuYanActivity.class);
                startActivity(intent);
                break;
            case R.id.go_info_ll:
            case R.id.tv_status:
                intent = new Intent(getActivity(), MyInfoActivity.class);
                intent.putExtra("type",0);
                startActivity(intent);
                break;
            case R.id.setting:
                startActivity(new Intent(context, SZActivity.class));
                break;
            case R.id.my_ask_ll:
                intent = new Intent(getActivity(), AskMannagerActivity.class);
                intent.putExtra("type",1);
                startActivity(intent);
                break;
            case R.id.my_answer_ll:

                intent = new Intent(getActivity(), AskMannagerActivity.class);
                intent.putExtra("type",3);
                startActivity(intent);
                break;
            case R.id.my_guanzhu_ll:
                intent = new Intent(getActivity(), GuanZhuActivity.class);
                intent.putExtra("type",1);
                startActivity(intent);
                break;
            case R.id.guanzhu_my_ll:

                intent = new Intent(getActivity(), GuanZhuActivity.class);
                intent.putExtra("type",2);
                startActivity(intent);
                break;
            case R.id.my_project:
                startActivity(new Intent(getActivity(), MyXM2Activity.class));
                break;
            case R.id.my_collection:
                startActivity(new Intent(getActivity(), ShouCangTmp.class));
                break;
            case R.id.about_us:
                Intent intent_web = new Intent(getActivity(), GZXYActivity.class);//额，
                intent_web.putExtra("title", "关于我们");
                intent_web.putExtra("id", 1);
                startActivity(intent_web);
                break;
            case R.id.my_message:
                startActivity(new Intent(context, MessageActivity.class));
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        requestUserInfo();
    }

    private void requestUserInfo(){
        Map<String,Object> params = new HashMap<>();
        params.put("code", HttpUtil.getParam());
        params.put("id", SPUtils.getUserId());
        HttpUtils.post(activity,URLAddress.URLBASE + URLAddress.URL_CENTER, params, new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {
                swiperefreshlayout.setRefreshing(false);
                SPUtils.setDatas(SPUtils.USERS_INTO,str);
                userInfo2 = JSON.parseObject(str,new TypeReference<UserInfo2>(){});
                initUser();
            }

            @Override
            public void onFailure(int requestCode, String str) {
                swiperefreshlayout.setRefreshing(false);
                DialogUtil.showToast(context,str);
            }
        });
    }
}
